# -*- coding: utf-8 -*-
import json
import ssl
import urllib2

import hashlib
import os
import datetime



ssl._create_default_https_context = ssl._create_unverified_context

def gettoken():
    requrl = "https://cmdbapi.bl.com/auth/token/"
    post_data = {
    "client_id": "GOLzAdKixWEmTPeSnNcwlvyoIhBa2b8qCs1ZkX6r",
    "client_secret": "843lIeaeb7R7fW309Ga5ateBYbKi328d365137w81d372f9WebNSbe0e58E6b6dVb05jOy9nQKunQ0iw4PO793Vjfst3eabq6q2b5Dfk6d2pa9aZUd5okbLLFc66UHX4",
    "grant_type": "password",
    "username": "cmdb@bl.com",
    "password": "3jDGrJh6z5qY"
    }
    url_request = urllib2.Request(url=requrl, data=json.dumps(post_data), headers={"Content-Type": "application/json"})
    url_open = urllib2.urlopen(url_request)
    url_info = eval(url_open.read())
    jksrestoken = url_info['access_token']
    jksrestoken =  "Bearer" + " " + jksrestoken
    return jksrestoken

result=gettoken()
print result

def getappinfo(AppName):
    result1 = gettoken()
    print AppName
    jksrequrl = "https://cmdbapi.bl.com/blapp/appnamelist/?appname=%s" %(AppName)
    print jksrequrl
    jksheaderdata = {"Authorization": str(result)}
    # print jksheaderdata
    jksurl_request = urllib2.Request(url=jksrequrl, headers=jksheaderdata)
    # print jksurl_request
    jksurl_open = urllib2.urlopen(jksurl_request)

    # print (----------'
    res =  jksurl_open.read()

    res_dict = json.loads(res)
    print res_dict

    for i in  res_dict['app_name']:
        print i['bus_ip']

getappinfo("product-admin")
# def GetFileMd5(filename):
#     if not os.path.isfile(filename):
#         return
#     myhash = hashlib.md5()
#     f = file(filename,'rb')
#     while True:
#         b = f.read(8096)
#         if not b :
#             break
#         myhash.update(b)
#     f.close()
#     return myhash.hexdigest()
#
# # filepath = raw_input('请输入文件路径：')
#
# # 输出文件的md5值以及记录运行时间
# starttime = datetime.datetime.now()
# print GetFileMd5("/root/abc/PRD-blgroup-osp-content/blgroup-osp-content-web/target/blgroup-osp-content.war")
# endtime = datetime.datetime.now()
# print '运行时间：%ds'%((endtime-starttime).seconds)
